package com.qing.study.comm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(Result.class);
    private boolean success = false;
    private String message = "操作失败！";
    private Integer a = 400;
    private T result;
    private final long timestamp = System.currentTimeMillis();

    public Result() {
    }

    public void error500(String message) {
        this.message = message;
        this.a = 500;
        this.success = false;
    }

    public void error401() {
        this.message = "TOKEN失效，请重新登录！";
        this.a = 401;
        this.success = false;
    }

    public void error403() {
        this.message = "越权访问，无此权限";
        this.a = 403;
        this.success = false;
    }

    public void error404() {
        this.message = "找不到接口！";
        this.a = 404;
        this.success = false;
    }

    public void error400(String message) {
        this.message = message;
        this.a = 400;
        this.success = false;
    }

    public void success(String message) {
        this.message = message;
        this.a = 200;
        this.success = true;
    }

    public void success206(String message) {
        this.message = message;
        this.a = 206;
        this.success = true;
    }


    public static <T> Result<T> error(int code, String msg) {
        Result<T> r = new Result();
        if (code != 200 && code != 400 && code != 401 && code != 403 && code != 404 && code != 500) {
            log.error("function=set result code, input code:{} is illegal", code);
            code = 500;
        }

        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static <T> Result<T> ok() {
        Result<T> r = new Result();
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage("成功");
        return r;
    }

    public static <T> Result<T> ok(String msg) {
        Result<T> r = new Result();
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result();
        r.setSuccess(true);
        r.setCode(200);
        r.setResult(data);
        r.setMessage("成功");
        return r;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setSuccess(boolean success) {
        this.success = success;
        if (success) {
            this.a = 200;
            this.message = "成功";
        }

    }

    public void setCode(int code) {
        if (code != 200 && code != 400 && code != 401 && code != 403 && code != 404 && code != 500 && code != 399) {
            log.error("function=set result code, input code:{} is illegal", code);
            code = 500;
        }

        this.a = code;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public T getResult() {
        return this.result;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getMessage() {
        return this.message;
    }

    public Integer getCode() {
        return this.a;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result) o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.isSuccess() != other.isSuccess()) {
                return false;
            } else {
                Object thisMessage = this.getMessage();
                Object otherMessage = other.getMessage();
                if (thisMessage == null) {
                    if (otherMessage != null) {
                        return false;
                    }
                } else if (!thisMessage.equals(otherMessage)) {
                    return false;
                }

                Object thisCode = this.getCode();
                Object otherCode = other.getCode();
                if (thisCode == null) {
                    if (otherCode != null) {
                        return false;
                    }
                } else if (!thisCode.equals(otherCode)) {
                    return false;
                }

                Object thisResult = this.getResult();
                Object otherResult = other.getResult();
                if (thisResult == null) {
                    if (otherResult != null) {
                        return false;
                    }
                } else if (!thisResult.equals(otherResult)) {
                    return false;
                }

                return this.getTimestamp() == other.getTimestamp();
            }
        }
    }

    private boolean canEqual(Object other) {
        return other instanceof Result;
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        Object message1 = this.getMessage();
        result = result * 59 + (message1 == null ? 43 : message1.hashCode());
        Object code1 = this.getCode();
        result = result * 59 + (code1 == null ? 43 : code1.hashCode());
        Object result1 = this.getResult();
        result = result * 59 + (result1 == null ? 43 : result1.hashCode());
        long timestamp1 = this.getTimestamp();
        result = result * 59 + (int) (timestamp1 >>> 32 ^ timestamp1);
        return result;
    }

    @Override
    public String toString() {
        return "Result(success=" + this.isSuccess() + ", message=" + this.getMessage() + ", code=" + this.getCode() + ", result=" + this.getResult() + ", timestamp=" + this.getTimestamp() + ")";
    }

}

